package team.unstudio.unknownrpg_base.attribute;

import java.util.HashMap;
import java.util.Map;

import org.bukkit.entity.Player;

import indi.defoliation.unknownrpg.api.attribute.Attribute;
import indi.defoliation.unknownrpg.api.attribute.AttributeBody;
import indi.defoliation.unknownrpg.api.attribute.AttributeEventHandler;
import indi.defoliation.unknownrpg.api.attribute.NumberAttribute;
import indi.defoliation.unknownrpg.api.attribute.SingleValueAttribute;
import indi.defoliation.unknownrpg.api.rpgevent.player.PlayerSetupEvent;

public class LevelLimitAttribute extends Attribute implements NumberAttribute<Integer>,SingleValueAttribute{

	private int level;
	
	public LevelLimitAttribute(int level) {
		set(level);
	}

	@Override
	public Map<String, Object> serialize() {
		HashMap<String,Object> map = new HashMap<>();
		map.put("levelLimit", level);
		return map;
	}

	@Override
	public void add(Integer t) {
		this.level+=t;
	}

	@Override
	public Integer get() {
		return level;
	}

	@Override
	public void set(Integer t) {
		this.level=t;
	}

	@Override
	public String getKeyName() {
		return "LevelLimit";
	}

	@Override
	public String getDisplayName() {
		return "等级限制";
	}

	@Override
	public void deserialize(Map<String, Object> map) {
		this.level=(int)map.get(getKeyName());
	}
	
	@Override
	public Attribute getAttribute() {
		return this;
	}
	
	@AttributeEventHandler
	public void onSetup(PlayerSetupEvent event,Map<AttributeBody,Attribute> map) {
		Player p = event.getEntity();
		if(!map.containsKey(event.getLocation()))return;
		int levelLimit = ((LevelLimitAttribute)map.get(event.getLocation())).get();
		if(p.getLevel()<levelLimit)event.setCancelled(this, true);
	}
}
